package com.example.bookkeeping1220.talk;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bookkeeping1220.R;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Msg> mMsgList;
    Drawable drawLeft, drawRight;

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rihgtMsg;
        ImageView headLeft, headRight;


        public ViewHolder(View view) {
            super(view);
            leftLayout = view.findViewById(R.id.left_layout);
            rightLayout = view.findViewById(R.id.right_layout);
            leftMsg = view.findViewById(R.id.left_msg);
            rihgtMsg = view.findViewById(R.id.right_msg);
            headLeft = view.findViewById(R.id.headPortraitLeft);
            headRight = view.findViewById(R.id.headPortraitRight);
        }
    }

    public MsgAdapter(List<Msg> msgList, Drawable left, Drawable right) {
        mMsgList = msgList;
        drawLeft = left;
        drawRight = right;
//        imag.setBounds(0,0,40,44);
//        ImageView head = findViewById(R.id.headPortraitLeft);
//        head.setImageDrawable(imag);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_talking, parent, false);
        ViewHolder holder = new ViewHolder(view);
        holder.headLeft.setImageDrawable(drawLeft);
        holder.headRight.setImageDrawable(drawRight);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msg msg = mMsgList.get(position);
        if (msg.getType() == Msg.TYPE_RECEIVED) {
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());
        } else if (msg.getType() == Msg.TYPE_SENT) {
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rihgtMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}